﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

using FCNS.KeepMoney.Configuration;
using FCNS.KeepMoney.Util;
using FCNS;
using FCNS.Translation;

namespace FCNS.KeepMoney.Forms
{
    public partial class LanguageForm : ParentForm
    {
        public LanguageForm()
        {
            InitializeComponent();
            Program.Translation.ApplyTo(this);
        }

        public void Init()
        {
            FCNS.Utility.BannerFactory.CreateBannerEx(this, pictureBox1, Properties.Resources.language_32,
               LanguageUtil.选择语言,LanguageUtil.在这里你可以选择另一种用户界面语言);

            if (!Directory.Exists(AppDefines.languagePath))
                Directory.CreateDirectory(AppDefines.languagePath);

            ListViewItem item = listView1.Items.Add(AppDefines.defaultLanguage);
            string[] str = AppDefines.version.Split('.');
            item.SubItems.Add(str[0]+"."+str[1]);
            item.SubItems.Add(AppDefines.fullName);
            item.SubItems.Add(AppDefines.webSite);
            LoadLanguageInfo();
        }

        private void OnMoreLanguage(object sender, EventArgs e)
        {
            AppDefines.WebSite.Show(AppDefines.WebSite.languageUrl);
        }

        private void OnLanguageChanged(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
                return;

            if (Program.AppConfig.App.Language == listView1.SelectedItems[0].Text)
                return;

            if (MessageBox.Show(LanguageUtil.确定加载新的语言文件, "", MessageBoxButtons.YesNo,
                MessageBoxIcon.Information) == DialogResult.Yes)
            {
                Program.AppConfig.App.Language = listView1.SelectedItems[0].Text;
                if (Program.AppConfig.App.Language == AppDefines.defaultLanguage)
                    MessageBox.Show(LanguageUtil.更新完成_请重启KeepMoney);
                else
                {
                    Program.LoadTranslation();
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
            }
        }

        private void LoadLanguageInfo()
        {
            foreach (string file in Directory.GetFiles(AppDefines.languagePath))
            {
                try
                {
                    Translate trl = Translate.LoadFromFile(file);
                    ListViewItem lvi = listView1.Items.Add(trl.Properties.NameNative);
                    lvi.SubItems.Add(trl.Properties.ApplicationVersion);
                    lvi.SubItems.Add(trl.Properties.AuthorName);
                    lvi.SubItems.Add(trl.Properties.AuthorContact);
                    lvi.Tag = file;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

    }
}
